package persistent.impl;

import java.sql.ResultSet;
import java.util.List;

import Util.Photostatus;
import basic.BaseDAO;
import model.TPhoto;
import persistent.dao.PhotoDAO;


public class PhotoDAOImpl implements PhotoDAO {
	private BaseDAO dao = null;
	
	public PhotoDAOImpl() {
		super();
		dao = new BaseDAO();
	}

	@Override
	public TPhoto getPhotoById(int id) {
		String sql = "select * from T_Photo where id = ?";
		Object[] para = {id};
		ResultSet rs = dao.select(sql, para);
		TPhoto photo = new TPhoto(rs);
		dao.close();
		return photo;
	}

	@Override
	
		public int addPhoto(TPhoto obj) {
		String sql="insert into T_Photo(photoname,status) values(?,?)";
		Object para[]={obj.getPhotoname(),obj.getStatus()};
		int row = dao.insert(sql, para);
		if(row!=0){
			return row;
		}
		else {
			return 0;
		}
		}

	@Override
	public boolean deleteAllPhotos(int status) {
		String sql = "update T_Photo set status = 1 where status = ? ";
		Object[] param={status};
		if (dao.delete(sql, param)>0) 
			return true;
			
		else 
			return false;
	}

	@Override
	public boolean desiablePhoto(int id) {
		String sql = "update T_Photo set status = ?  where id = ?";
		Object[] param={id,Photostatus.disable};
		if (dao.delete(sql, param)>0) 
			return true;
			
		else 
			return false;
	}

	@Override
	public boolean deletePhoto(int id) {
		String sql = "delete from  T_Photo  where id = ?";
		Object[] param={id};
		if (dao.delete(sql, param)>0) 
			return true;
			
		else 
			return false;
	}

	@Override
	public List<TPhoto> getPhotos(int status) {
		String sql = "";
		if (status == 0) {
			sql = "select * from T_Photo";
			ResultSet rs = dao.select(sql);
			List<TPhoto> list =TPhoto.toList(rs);
			dao.close();
			return list;
			
		}else {
			sql = "select * from T_Photo where status = ?";
			Object []param={status};
			ResultSet rs = dao.select(sql, param);
			List<TPhoto> list =TPhoto.toList(rs);
			dao.close();
			return list;
			
		}
		
			
		
	}

	@Override
	public boolean enablePhoto(int id) {
		String sql = "update T_Photo set status = ?  where id = ?";
		Object[] param={id,Photostatus.enable};
		if (dao.delete(sql, param)>0) 
			return true;
			
		else 
			return false;
	}

}
